import unittest

from xlwings import Book

from autoTask.core import taskMapping
from autoTask.taobao.sycm.gjc.competitorGoodKey import GoodsFeature, CategorySearch
from autoTask.taobao.sycm.gjc.competitorGoodKey.getCompetingKeyAction.AppendXgfxDataAction import AppendXgfxDataAction
from autoTask.taobao.sycm.utils.close_Ad import close_ad
from common.utils import web_page_util, FileUtil, ExcelUtil
from autoTask.taobao.sycm.gjc.competitorGoodKey.findCompetingProductsAction.find_competing_products import \
    FindCompetingProducts
from autoTask.taobao.sycm.gjc.competitorGoodKey import CompetitorGoodKey
from common.logger.log import log_
from common.utils.ThreadUtil import before_enable_stop_abel_thread
from common.utils.web_page_util import MockTap


class CompetitorGoodKeyTest(unittest.TestCase):
    def setUp(self):
        self.page = web_page_util.create_page()
        self.contain_name_list = ['麦饭石']
        self.ignore_name_list = ['八角']
        self.ignore_keys = ['官方', '旗舰', 'hzib', '苏泊尔', '赫巴兹', '赫', '康巴', "抱抱", '山', '懒米']
        self.param_dict = {
            "isTestExecute": True,
            "taskId": "5",
            "taskName": "生意参谋-竞争对手好词",
            "param": {
                "tbAccount": "赫巴兹直销企业店:数据",
                "tbPassword": "aa123456",
                "ignoreKeys": [
                    "旗舰",
                    "hzib",
                    "苏泊尔",
                    "赫巴兹",
                    "赫",
                    "康巴",
                    "官方",
                    "抱抱",
                    "山",
                    "懒米",
                    "粉色",
                    "素味"
                ],
                "competingMinVisits": 10,
                "competingMinRoi": 0,
                "categorySearchList": [
                    {
                        "name": "炒锅",
                        "ignoreNameList": [
                            "八角"
                        ],
                        "containNameList": [
                            "麦饭石"
                        ],
                        "goodsFeatureList": [
                            {
                                "productId": "708371953374",
                                "priceScope": [
                                    100,
                                    300
                                ],
                                "competing1": "687701902727",
                                "competing2": "612895297846"
                            },
                        ],
                        "dateCycle": "30天"
                    }
                ],
                "keyIsNotContainLetter": True
            }
        }

    def test_run_pro(self):
        """
        模拟生产测试
        :return:
        """
        category_search_list = [CategorySearch('炒锅', ignore_name_list=self.ignore_name_list
                                               , contain_name_list=self.contain_name_list)]
        CompetitorGoodKey(self.page, ignore_keys=self.ignore_keys, competing_min_visits=10, is_test_execute=True,
                          category_search_list=category_search_list, key_is_not_contain_letter=True)()

    @before_enable_stop_abel_thread(target_func=close_ad, is_loop=True)
    def test_get_my_goods_features(self):
        """
        测试获取本店商品特征
        :return:
        """
        goods_features = CompetitorGoodKey(self.page, []).get_my_goods_features(
            category_search=CategorySearch("炒锅", contain_name_list=self.contain_name_list))
        log_.info(f"本店商品特征 : {goods_features}")

    @before_enable_stop_abel_thread(target_func=close_ad, is_loop=True)
    def test_find_competing_products(self):
        goods_features = [GoodsFeature(goods_id=644024927427, price_scope=[150, 300])]
        log_.info(FindCompetingProducts(page=self.page, goods_features=goods_features, category="炒锅",
                                        date_cycle="30天", contain_name_list=self.contain_name_list,
                                        ignore_name_list=self.ignore_name_list)())

    def test_get_competing_key(self):
        path = FileUtil.get_project_root() + r"/taskTemplateExcel/gjc/competitorGoodKey/result/炒锅_待追加.xlsx"
        sheet = Book(path).sheets[0]
        AppendXgfxDataAction(sheet=sheet, date_cycle="30", p=self.page)()

    def test_pro(self):
        taskMapping.competitor_good_key(self.param_dict)

    def test_(self):
        print(self.page.eles("竞争配置")[1])
        MockTap(self.page("x://li[@class='menuItem level-leaf selected  leaf']//span[@class='name']"),
                "跳转到 [竞争配置] 标签页失败", before_delay=(1, 3))()


if __name__ == '__main__':
    unittest.main()
